package com.mobile.business.grid;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.mobile.dao.HibernateSessionFactory;


public class GridBusinessProxy implements InvocationHandler {
	
	private Object objOriginal;
	public GridBusinessProxy(Object obj){
		this.objOriginal=obj;
	}
	/**
	 * 实际执行的方法
	 */
	public Object invoke(Object proxy, Method method, Object[] args)
		throws Throwable {
		// TODO Auto-generated method stub
		Object result=null;
		Session session=HibernateSessionFactory.getSession();
		Transaction transaction=null;
		try {
			transaction=session.getTransaction();
			transaction.begin();
			result=method.invoke(this.objOriginal, args);
			transaction.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			HibernateSessionFactory.rollback(transaction);
			throw new RuntimeException();
		} catch(Exception e){
			e.printStackTrace();
			HibernateSessionFactory.rollback(transaction);
			throw new RuntimeException();
		} finally{
			HibernateSessionFactory.closeSession();
		}
		
		
		return result;
	}

}
